<%@ page import="java.util.Collection"%>
<%@ page import="java.util.Iterator"%>
<%@ page import="pizza.admin.entity.User"%>
<%@ page import="pizza.admin.entity.Order"%>
<%@ page import="pizza.admin.entity.OrderPizza"%>
<%@ page import="pizza.admin.entity.Pizza"%>
<%
	User adminUser = null;

	if (session.getAttribute("user") != null) {
		adminUser = (User) session.getAttribute("user");
	}

	if (adminUser == null) {
		response.sendRedirect("/");
	}

	Order order = new Order();

	if (request.getAttribute("order") != null) {
		order = (Order) request.getAttribute("order");
	}
%>
<jsp:include page="../../layout/header.jsp" flush="true" />
<div class="container">

	<h3>Orders</h3>
	<div class="main row-fluid">

		<%
			if (request.getMethod() == "POST" && !order.validateForDB()) {
		%>
		<div class="alert alert-error">All fields with * are required !</div>
		<%
			}
		%>
		<span class="span12 ">
			<form name='' method='post' action=''>
				<label class="control-label" for="inputAddress">Address *</label>

				<div class="controls">
					<input name="address" type="text" id="inputAddress"
						value='<%=order.getAddress()%>'>
				</div>

				<label class="control-label" for="inputPhone">Phone *</label>
				<div class="controls">
					<input name="phone" type="text" id="inputPhone"
						value='<%=order.getPhone()%>'>
				</div>

				<label class="control-label" for="inputDescription">User*: </label>
				<div class="controls">
					<select name="user" id="inputDescription">
						<%
							Collection<User> allUsers = (Collection<User>) request
									.getAttribute("users");

							if (allUsers != null && allUsers.size() > 0) {
								Iterator objectsIterator = allUsers.iterator();
								while (objectsIterator.hasNext()) {
									User user = (User) objectsIterator.next();
						%>
						<option value='<%=user.getId().toString()%>'
							<%=(user.getId() == order.getUser().getId()) ? "selected=selected"
							: ""%>><%=user.getName() + " " + user.getSurname()%></option>
						<%
							}
							}
						%>
					</select>
				</div>

				<label class="control-label" for="inputDescription">Description
				</label>
				<div class="controls">
					<input name="description" type="text" id="inputDescription"
						value='<%=order.getDescription()%>'>
				</div>
				<div class="controls">
					<br> <input type="submit" name="submit" id="submit"
						value='Save' class='btn btn-primary'>
				</div>
			</form>
			<hr>
			<h3>Ordered pizzas</h3>
			<table class="table table-bordered table-hover">
				<thead>
					<tr>
						<th>Pizza</th>
						<th>Count</th>
						<th>Price</th>
						<!-- <th>&nbsp;</th> -->
					</tr>
				</thead>
				<%
					Collection<OrderPizza> orderedPizas = (Collection<OrderPizza>) order
							.getOrederPizas();

					if (orderedPizas != null && orderedPizas.size() > 0) {
						Iterator orderedPizzasIterator = orderedPizas.iterator();
						while (orderedPizzasIterator.hasNext()) {
							OrderPizza orderedPizza = (OrderPizza) orderedPizzasIterator
									.next();
							Double price = orderedPizza.calculateFullPrice();
				%>
				<tr>
		<td><%=orderedPizza.getPizza().getName()%></td>
		<td><%=orderedPizza.getCount().toString()%></td>
		<td><%=price.toString()%></td>
		<%-- <td>
			<a href='javascript:;' class='btn btn-danger delete'>Delete</a>
			<div class='clearfix alert hidden-content delete-confirmation'>
				Confirm deletion
				<a href='/order?action=delete&id=<%=orderedPizza.getId().toString() %>' class='btn btn-danger'>Yes</a>
				<button type="button" class="close decline-deletion">&times;</button>
			<div>
		</td> --%>
	</tr>
				<%
						}
					}
				%>
				<tr>
					<td colspan='2'>Total: </td>
					<td><%=order.calculateFullPrice().toString() %></td>
					<!-- <td></td> -->
				</tr>
			</table>
		</span>
	</div>
</div>
<!-- /container -->

<jsp:include page="../../layout/footer.jsp" flush="true" />